<template>
  <div id="mylogin">
    <h1>登录页面</h1>
    <div id="fromdiv">
      <el-form ref="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="password" type="password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login()">登录</el-button>
          <el-button native-type="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>


  export default {
    name: "Login",
    data() {
      return {
        username: "",
        password: ""
      }
    },
    methods: {
      //登录的方法11111
      login(){
        // this.username;
        // this.password;
        var vt = this;
        this.utils.myajax({
          type: "POST",
          url: this.utils.myurl.login,
          data: {
            username: this.username,
            password: this.password,
            grant_type: "password",
            client_id: "system",
            client_secret: "system",
            scope: "all"
          },
          success: function(data){
            //获取认证通过后的令牌
            var accessToken = data.access_token;
            var refreshToken = data.refresh_token;
            //保存到localStorage中
            localStorage.setItem("accessToken", accessToken);
            localStorage.setItem("refreshToken", refreshToken);
            //跳转到首页
            vt.$router.push("/Home");
          },
          exception: function(resp){
            if (resp.status == 400 || resp.status == 401){
              vt.$message.error('账号或密码错误');
            }
          }
        });
      }
    }
  }
</script>

<style>
 #fromdiv {
   width: 30%;
   height: 50%;
   margin: 0 auto;
 }
</style>
